package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.common.model.course.CourseVideo;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.vo.CourseVideoVo;
import com.zbkj.service.dao.CourseVideoDao;
import com.zbkj.service.service.CourseVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程视频服务实现类
 */
@Service
public class CourseVideoServiceImpl extends ServiceImpl<CourseVideoDao, CourseVideo> implements CourseVideoService {

    @Resource
    private CourseVideoDao dao;

    /**
     * 分页获取课程视频列表
     * @param courseId 课程ID
     * @param pageParamRequest 分页参数
     * @return List<CourseVideo>
     */
    @Override
    public List<CourseVideo> getList(Integer courseId, PageParamRequest pageParamRequest) {
        LambdaQueryWrapper<CourseVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseVideo::getCourseId, courseId);
        queryWrapper.eq(CourseVideo::getIsDel, false);
        queryWrapper.orderByAsc(CourseVideo::getEpisodeNumber);
        return dao.selectList(queryWrapper);
    }

    /**
     * 保存课程视频
     * @param courseVideo 课程视频对象
     * @return Boolean
     */
    @Override
    public Boolean saveVideo(CourseVideo courseVideo) {
        courseVideo.setAddTime((int) (System.currentTimeMillis() / 1000));
        courseVideo.setUpdateTime((int) (System.currentTimeMillis() / 1000));
        courseVideo.setIsDel(false);
        return save(courseVideo);
    }

    /**
     * 更新课程视频
     * @param courseVideo 课程视频对象
     * @return Boolean
     */
    @Override
    public Boolean updateVideo(CourseVideo courseVideo) {
        courseVideo.setUpdateTime((int) (System.currentTimeMillis() / 1000));
        return updateById(courseVideo);
    }

    /**
     * 删除课程视频
     * @param id 视频ID
     * @return Boolean
     */
    @Override
    public Boolean deleteVideo(Integer id) {
        CourseVideo courseVideo = new CourseVideo();
        courseVideo.setId(id);
        courseVideo.setIsDel(true);
        courseVideo.setUpdateTime((int) (System.currentTimeMillis() / 1000));
        return updateById(courseVideo);
    }

    /**
     * 根据课程ID获取所有视频
     * @param courseId 课程ID
     * @return List<CourseVideoVo>
     */
    @Override
    public List<CourseVideoVo> getByCourseId(Integer courseId) {
        LambdaQueryWrapper<CourseVideo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseVideo::getCourseId, courseId);
        queryWrapper.eq(CourseVideo::getIsDel, false);
        queryWrapper.eq(CourseVideo::getStatus, true);
        queryWrapper.orderByAsc(CourseVideo::getSort).orderByAsc(CourseVideo::getEpisodeNumber);

        List<CourseVideo> list = dao.selectList(queryWrapper);
        List<CourseVideoVo> voList = new ArrayList<>();

        for (CourseVideo item : list) {
            CourseVideoVo vo = new CourseVideoVo();
            BeanUtils.copyProperties(item, vo);

            // 格式化时长
            if (item.getDuration() != null) {
                int minutes = item.getDuration() / 60;
                int seconds = item.getDuration() % 60;
                vo.setDurationFormat(String.format("%02d:%02d", minutes, seconds));
            } else {
                vo.setDurationFormat("00:00");
            }

            voList.add(vo);
        }

        return voList;
    }
}